package com.xiaoxin.codesandbox;

import com.xiaoxin.codesandbox.modle.ExecuteCodeRequest;
import com.xiaoxin.codesandbox.modle.ExecuteCodeResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/codesandbox")
public class MainController {

    private String AUTH_REQUEST_HEADER="header";

    private String AUTH_REQUEST_SECRET="secret";

    @Resource
    private JavaNativeCodeSandBoxImpl javaNativeCodeSandBoxImpl;

    @PostMapping("execute")
    public ExecuteCodeResponse executeCode(@RequestBody ExecuteCodeRequest executeCodeRequest, HttpServletRequest request, HttpServletResponse response){
        String header = request.getHeader(AUTH_REQUEST_HEADER);
        if(!AUTH_REQUEST_SECRET.equals(header)){
            response.setStatus(403);
            return null;
        }
        return javaNativeCodeSandBoxImpl.doExecute(executeCodeRequest);
    }
}
